package com.nd.adjust;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Snapshot {

	// private
	private IPickFromHtml pickKeyFromHtml;

	public static void main(String[] args) {
		Snapshot snapshot = new Snapshot();
		snapshot.snatch("http://www.haha.mx/new/1",
				"com.nd.adjust.impl.PickFromHaHaMx");
		snapshot.snatch("http://www.haha.mx/new/2",
				"com.nd.adjust.impl.PickFromHaHaMx");
	}

	public Snapshot() {
	}

	public void snatch(String url, String dealClassName) {
		pickKeyFromHtml = getDealClass(dealClassName);
		try {
			Document doc = Jsoup.connect(url).get();
			pickKeyFromHtml.accept(doc);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private IPickFromHtml getDealClass(String dealClassName) {
		// TODO Auto-generated method stub
		if (pickKeyFromHtml != null) {
			String pickName = pickKeyFromHtml.getClass().getName();
			if (pickName.equals(dealClassName)) {
				return pickKeyFromHtml;
			}
		}
		try {
			Object object = Class.forName(dealClassName).newInstance();
			if (object instanceof IPickFromHtml) {
				return (IPickFromHtml) object;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
